package cn.otrue.patienthealthmanager.xmpp;

import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.provider.ProviderManager;
import org.jivesoftware.smackx.OfflineMessageManager;
import org.jivesoftware.smackx.bytestreams.socks5.provider.BytestreamsProvider;
import org.jivesoftware.smackx.filetransfer.FileTransferManager;
import org.jivesoftware.smackx.provider.DiscoverInfoProvider;
import org.jivesoftware.smackx.provider.DiscoverItemsProvider;

import cn.otrue.patienthealthmanager.utils.MyConst;

import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;

public class XmppTool {

	private static XMPPConnection con = null;
	/***/
	public static String USERJID = "";

	private static XMPPListener xmppListener = new XMPPListener();

	private static void openConnection() throws XMPPException {
		ConnectionConfiguration connConfig = new ConnectionConfiguration(
				XmppConst.URL, XmppConst.HOST);
		connConfig.setSendPresence(false);
		// 服务器 ConnectionConfiguration connConfig = new
		// ConnectionConfiguration("192.168.1.113", 5222);
		con = new XMPPConnection(connConfig);

		con.connect();
		ProviderManager.getInstance().addIQProvider("query",
				"http://jabber.org/protocol/bytestreams",
				new BytestreamsProvider());
		ProviderManager.getInstance().addIQProvider("query",
				"http://jabber.org/protocol/disco#items",
				new DiscoverItemsProvider());
		ProviderManager.getInstance().addIQProvider("query",
				"http://jabber.org/protocol/disco#info",
				new DiscoverInfoProvider());

	}

	public static boolean isConnection() {
		if (con == null) {
			return false;
		}
		return true;
	}

	public static XMPPConnection getConnection() throws XMPPException {
		if (con == null) {
			openConnection();
			USERJID = con.getUser();

		}
		return con;
	}

	public static void closeConnection() {
		if (con != null && con.isConnected()) {
			// 连接监听
			// con.removeConnectionListener(xmppListener);
			// 消息监听
			con.getChatManager().removeChatListener(xmppListener);
			con.disconnect();
			con = null;

		}

	}

	/**
	 * 登录open
	 * 
	 * @param username
	 * @param password
	 * @param handler
	 */
	public static void loginOpenFire(final String username,
			final String password, final Handler handler) {
		Thread t = new Thread(new Runnable() {

			public void run() {
				try {

					// 连接
					XmppTool.closeConnection();
					XMPPConnection connection = XmppTool.getConnection();
					Log.e("xmpp", username + "  " + password);
					try {
						connection.login(username, password);
					} catch (Exception e1) {
						XmppTool.closeConnection();

					}

					// 添加连接监听
					// connection.addConnectionListener(xmppListener);

					// 消息监听
					connection.getChatManager().addChatListener(xmppListener);
					// 文件
					FileTransferManager fileTransferManager = new FileTransferManager(
							connection);

					fileTransferManager.addFileTransferListener(xmppListener);

					// 离线消息获取
					OfflineMessageManager offlineManager = new OfflineMessageManager(
							connection);

					offlineManager.getMessages();

					// 清除离线消息

					offlineManager.deleteMessages();

					// 上线
					Presence presence = new Presence(Presence.Type.available);
					connection.sendPacket(presence);
					handler.sendEmptyMessage(1);
				} catch (Exception e) {
					XmppTool.closeConnection();
					handler.sendEmptyMessage(2);
				}
			}
		});
		t.start();
	}

	public static boolean addUser(Roster roster, String userName, String name,
			String groupName) {
		try {
			roster.createEntry(userName, name, new String[] { groupName });
			return true;
		} catch (Exception e) {
			return false;
		}
	}

}
